package com.kobeliu.netty.websocket;

import io.netty.channel.Channel;
import org.checkerframework.checker.units.qual.C;

import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会话管理
 * 用户Id和channel的管理关系处理
 */
public class UserChannelSession {

    //多端同账号（id）进行管理
    private static Map<String, List<Channel>> multiSession = new HashMap<>();

    //用户ID与channelLongId 的 关联关系
    //userId不可以在webSocket通信中直接获得
    private static Map<String, String> multiUSerIdAndLongId = new HashMap<>();

    public static void putLongIdAndUserId(String userId, String longId){
        multiUSerIdAndLongId.put(longId,userId);
    }

    public static String getUserId(String longId){
        return multiUSerIdAndLongId.get(longId);
    }

    public static void putUserIdAndChannels(String userId, Channel channel){
        List<Channel> channels = multiSession.get(userId);
        if(channels==null || channels.size()==0){
            channels = new ArrayList<>();
            channels.add(channel);
            multiSession.put(userId,channels);
        }else{
            if(!channels.contains(channel)){
                channels.add(channel);
                multiSession.put(userId,channels);
            }
        }
     //   System.out.println("multiSession.get(userId)"+multiSession.get(userId).size());
    }

    public static void deleteChannelByUserId(String userId, Channel channel){
        if(userId!=null && channel!= null){
            List<Channel> channelList = multiSession.get(userId);
            if(channelList!=null && channelList.size()!=0){
                if(channelList.contains(channel)){
                    for (int i = 0; i < channelList.size(); i++) {
                        if(channelList.get(i).id().asLongText().equals(channel.id().asLongText())){
                            channelList.remove(i);
                            break;
                        }
                    }
                }
            }
            if(channelList == null || channelList.size() == 0){
                multiSession.remove(userId);
            }else {
                multiSession.put(userId,channelList);
            }
        }


    }

    public static void removeLongIdAndUserId(String userId,String longId){
        multiUSerIdAndLongId.remove(longId);
    }

    public static List<Channel> getChannels(String userId){
        List<Channel> channels = multiSession.get(userId);
        return channels;
    }

    public static void outputMulti() {
        System.out.println("===================================");

        for (HashMap.Entry<String,List<Channel>> entry:multiSession.entrySet()){

            System.out.println("userID:"+entry.getKey());
            System.out.println("channels:"+entry.getValue().toString());
        }

        System.out.println("===================================");
    }

    /**
     * 获得自己其他端的channel
     * 在我发送消息时，同步给其他设备
     * @param userId
     * @param channelId
     * @return
     */
    public static List<Channel> getMyOtherChannels(String userId,String channelId){

        List<Channel> channels = getChannels(userId);
        if(channels==null || channels.size()==0){
            return new ArrayList<>();
        }

        List<Channel> temp = new ArrayList<>();
        for (int i = 0; i < channels.size(); i++) {
            Channel channel = channels.get(i);
            if(!channel.id().asLongText().equals(channelId)){
                temp.add(channel);
            }
        }
        return temp;

    }
}
